package com.learnjava.learnjava1.service;

import com.learnjava.learnjava1.dto.CompanyDTO;
import com.learnjava.learnjava1.dto.DepartmentDTO;
import com.learnjava.learnjava1.dto.DepartmentShortDTO;
import com.learnjava.learnjava1.jpa.entity.CompanyEntity;
import com.learnjava.learnjava1.jpa.entity.DepartmentEntity;
import com.learnjava.learnjava1.jpa.repository.CompanyRepository;
import com.learnjava.learnjava1.jpa.repository.DepartmentRepository;
import com.learnjava.learnjava1.jpa.specification.CompanySpecification;
import com.learnjava.learnjava1.jpa.specification.DepartmentSpecification;
import com.learnjava.learnjava1.map.DepartmentMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class DepartmentService {

    @Autowired
    DepartmentRepository departmentRepository;
    CompanyRepository companyRepository;
    DepartmentMapper departmentMapper;

    public DepartmentDTO create(DepartmentDTO departmentDto) {
        return departmentMapper.toDto(departmentRepository.save(departmentMapper.toEntity(departmentDto)));
    }

    public DepartmentDTO getDepartmentById(Long id) {
        return departmentRepository.findById(id)
                .map(departmentMapper::toDto)
                .orElse(new DepartmentDTO());
    }


    public Page<DepartmentDTO> readAll(Pageable pageable) {
        Page<DepartmentEntity> departmentEntityPage = departmentRepository.findAll(pageable);
        List<DepartmentDTO> departmentDTOList = departmentMapper.toDepartmentsDTOList(departmentEntityPage.getContent());
        return new PageImpl<>(departmentDTOList, pageable, departmentEntityPage.getTotalElements());
    }

    public Page<DepartmentDTO> filterAll(String title, Boolean active, Long id, Long companyId, Pageable pageable) {
        Page<DepartmentEntity> departmentEntityPage = departmentRepository.findAll(DepartmentSpecification.parseFilters(title, active, id, companyId), pageable);
        List<DepartmentDTO> companyDTOList = departmentMapper.toDepartmentsDTOList(departmentEntityPage.getContent());
        return new PageImpl<>(companyDTOList, pageable, departmentEntityPage.getTotalElements());

    }

    public DepartmentShortDTO update(DepartmentShortDTO departmentShortDTO, Long id) {
        DepartmentEntity departmentEntity = departmentRepository.findById(id).orElseThrow(() -> {
            throw new RuntimeException("Department not found -" + id);
        });

        CompanyEntity companyEntity = companyRepository.findById(departmentShortDTO.getCompanyId()).orElseThrow(() -> {
            throw new RuntimeException("Company not found -" + id);
        });

        departmentEntity.setCompany(companyEntity);
        departmentMapper.update(departmentShortDTO, departmentEntity);
        departmentRepository.save(departmentEntity);

        return departmentMapper.toShortDto(departmentEntity);
    }

    public void delete(Long id) {
        departmentRepository.deleteById(id);
    }

}
